/**
 * 
 */
package com.amoby.main.service.impl;

import java.util.List;
import java.util.Map;

import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.amoby.main.entity.BusinessType;
import com.amoby.main.mapper.BusinessTypeMapper;
import com.amoby.main.service.BusinessTypeService;

/**
 * @Copyright: 本内容仅限于重庆爱赢科技有限公司内部使用，禁止转发.
 * @Author: daijiangguo 2015年1月20日 上午10:15:24
 * @Version: $Id$
 * @Desc: <p>
 *        </p>
 */
@Service
public class BusinessTypeServiceImpl implements BusinessTypeService {
    @Autowired
    private BusinessTypeMapper   businessTypeMapper;

    @Override
    public List<Map<String, Object>> getAllBusinessTypes() {
        return businessTypeMapper.getAllBusinessTypes();
    }

    @Override
    public int insertBusinessType(BusinessType businessType) {
        if (StringUtils.isBlank(businessType.getCode())) {
            List<String> list = businessTypeMapper.queryUsedCodes(businessType.getParentId());
            String prentCode = "";
            if (businessType.getParentId() != null) {
                BusinessType parent = businessTypeMapper.selectByPrimaryKey(businessType.getParentId());
                prentCode = parent.getCode();
            }
            for (int i = 1; i < 1000; i++) {
                String tempCode = (1000 + i + "").substring(1);
                if (!list.contains(tempCode)) {
                    businessType.setCode(prentCode + tempCode);
                    break;
                }
            }
        }
        int i = businessTypeMapper.insertSelective(businessType);
        return i;
    }

    @Override
    public int updateBusinessType(BusinessType businessType) {
        if (StringUtils.isBlank(businessType.getCode())) {
            List<String> list = businessTypeMapper.queryUsedCodes(businessType.getParentId());
            String prentCode = "";
            if (businessType.getParentId() != null) {
                BusinessType parent = businessTypeMapper.selectByPrimaryKey(businessType.getParentId());
                prentCode = parent.getCode();
            }
            for (int i = 1; i < 1000; i++) {
                String tempCode = (1000 + i + "").substring(1);
                if (!list.contains(tempCode)) {
                    businessType.setCode(prentCode + tempCode);
                    break;
                }
            }
        }
        return businessTypeMapper.updateByPrimaryKeySelective(businessType);
    }

    @Override
    public int deleteBusinessType(Integer id) {
        return businessTypeMapper.deleteCaseChildren(id);
    }

    @Override
    public Long selectIdByCode(String code) {
        return businessTypeMapper.selectIdByCode(code);
    }

    @Override
    public List<Map<String, Object>> queryByParentId(Long parentId) {
        return businessTypeMapper.queryByParentId(parentId);
    }

    @Override
    public List<Map<String, Object>> queryParent() {
        return businessTypeMapper.queryParent();
    }

    @Override
    public List<BusinessType> queryParentType() {
        return businessTypeMapper.selectParentType();
    }

    @Override
    public List<BusinessType> querySubType(Long parentId) {
        return businessTypeMapper.selectSubType(parentId);
    }
}
